package com.chinasoft.ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.FileReader;

/**
 * @author 中软zhang
 * @create 2023-07-26 16:52
 */
public class GamePanel extends JPanel implements KeyListener {

    private Image[] bases = new Image[10];
    final int WIDTH = 20;
    int map[][] = new int[WIDTH][WIDTH]; //
    boolean manFlag = true;
    int manX, manY;
    int manStatus = 5;

    public GamePanel() {
        initBases();
        loadMap();
        // 显示map对象
//        displayMap();
        getManXY();
        setFocusable(true);
        setBackground(Color.GRAY);
//        setBackground(Color.RED);
        setBounds(0, 0, 1000, 1000);
//        manX = 8;
//        manY = 8;
        map[manY][manX] = 5;
        // 对键盘动作进行监听
        addKeyListener(this);
        setVisible(true);
        requestFocus();

       /* for (Image im : bases) {
            System.out.println(im);
        }*/
    }

    public void initBases() {
        for(int i = 0; i < 10; i++) {
            bases[i] = Toolkit.getDefaultToolkit().getImage(GamePanel.class.getResource("/image/scene/" + i + ".gif"));
        }
    }

    // 读 maps/10.map 文件
    public void loadMap() {
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(GamePanel.class.getResource("/maps/10.map").getPath());
            br = new BufferedReader(fr);
            String line = null;
            int y = 0;
            while( (line = br.readLine()) != null) {
                System.out.println(line);
                for(int x = 0; x < line.length(); x++) {
                    // 获取 char 对象  '0'
                    char ch = line.charAt(x);
                    // 将char 转换为 int
                    this.map[y][x] = Integer.parseInt("" + ch);
                }
                // 下一行
                y++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 通过地图数据获取 人的位置
     */
    public void getManXY() {
        for(int y = 0; y < WIDTH; y++) {
            for(int x = 0; x < WIDTH; x++) {
                int type = this.map[y][x];
                if (type == 5) {
                    this.manY = y;
                    this.manX = x;
                    break;
                }
            }
        }
    }

    public void displayMap() {
        for(int y = 0; y < WIDTH; y++) {
            for(int x = 0; x < WIDTH; x++) {
                int type = this.map[y][x];
                System.out.print(type + " ");
            }
            System.out.println("");
        }
    }

    @Override
    public void paint(Graphics g) {
        for(int y = 0; y < WIDTH; y++) {
            for(int x = 0; x < WIDTH; x++) {
                int type = this.map[y][x];
                g.drawImage(bases[type], x*50, y*50, this);
            }
        }
//        if (this.manFlag == true) {
//            g.drawImage(bases[this.manStatus], manX*50, manY*50, this);
//        }
    }

    private void moveRight() {
        if (manX < 19) {
            map[manY][manX] = 0;
            manX ++;
            map[manY][manX] = 7;
        }
//        System.out.println("man X is " + manY);
    }

    private void moveLeft() {
        if (manX >= 1) {
            map[manY][manX] = 0;
            manX --;
            map[manY][manX] = 6;
        }
//        System.out.println("man X is " + manY);
    }

    private void moveUp() {
        if (manY >= 1) {
            map[manY][manX] = 0;
            manY --;
            map[manY][manX] = 8;
        }
//        System.out.println("man X is " + manY);
    }

    private void moveDown() {
        if (manY < 19) {
            map[manY][manX] = 0;
            manY ++;
            map[manY][manX] = 5;
        }
//        System.out.println("man X is " + manY);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
//        System.out.println(e);
//        System.out.println(e.getKeyCode());
        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                moveUp();
                repaint();
                break;
            case KeyEvent.VK_DOWN:
                moveDown();
                repaint();
                repaint();
                break;
            case KeyEvent.VK_LEFT:
                moveLeft();
                repaint();
                break;
            case KeyEvent.VK_RIGHT:
                moveRight();
                repaint();
                break;
            default:
                break;
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}
